package alphabeta;

import game.Cell;
import game.HexBoard;
import game.Location;

import java.util.ArrayList;

public class Utility {
	

  //getAllPossibleMoves(): build list of successor moves
  public static ArrayList<Location> getAllPossibleMoves(HexBoard hexBoard) {
    ArrayList<Location> moveList = new ArrayList<Location>();
    for(int y = 0; y < hexBoard.getSize(); y++) {
      for(int x = 0; x < hexBoard.getSize(); x++) {
        if(hexBoard.isEmpty(hexBoard.getCell(x, y))) {
          moveList.add(new Location(x, y));
        }
      }
    }
    return moveList;
  } 
    

  //getPossibleMoves(): build list of successor moves
	public static ArrayList<Location> getPossibleMoves(HexBoard hexBoard) {
			return /* hexBoard.getSize() > 8 
			            ? getSubsetOfPossibleMoves(hexBoard)
			            :*/ 
			              getAllPossibleMoves(hexBoard);
	}
	
	
	public static ArrayList<Location> getSubsetOfPossibleMoves(HexBoard hexBoard) {
		ArrayList<Cell> pastMoveList = new ArrayList<Cell>();
		ArrayList<Location> moveList = new ArrayList<Location>();
		//list of past moves
		for(int i = 0; i < hexBoard.getSize(); i++) {
			for(int j = 0; j < hexBoard.getSize(); j++) {
				if(!hexBoard.isEmpty(hexBoard.getCell(i,j))) {
					pastMoveList.add(hexBoard.getCell(i,j));
				}
			}
		}
		
		//1st time, pick random places 25% of board space
		if(pastMoveList.size() == 1) {
			for(int i = 0; i < hexBoard.getSize()/4; i++) {
				int x = (int)(hexBoard.getSize() * Math.random());
				int y = (int)(hexBoard.getSize() * Math.random());
				Location loc = new Location(x, y);
				if(!moveList.contains(loc) && 
					hexBoard.isEmpty(hexBoard.getCell(x, y)))
					moveList.add(loc);
			}
		}
		
		for(int i = 0; i < pastMoveList.size(); i++) {
			Cell cell = pastMoveList.get(i);
			
			//level 1			
			ArrayList<Cell> neighborList =  (ArrayList<Cell>) hexBoard.getCellNeighbors(cell);
			for(int j = 0; j < neighborList.size(); j++) {
				Cell neighborCell = neighborList.get(j);
				if(neighborCell.getX() > -1 && neighborCell.getX() < hexBoard.getSize() &&
				   neighborCell.getY() > -1 && neighborCell.getY() < hexBoard.getSize() &&
				   hexBoard.isEmpty(neighborCell)) {
					Location loc = new Location (neighborCell.getX(), neighborCell.getY());
					if(!moveList.contains(loc))
						moveList.add(loc);
				}
				
				//level 2
				ArrayList<Cell> neighborList2 =  (ArrayList<Cell>) hexBoard.getCellNeighbors(neighborCell);				
				for(int k = 0; k < neighborList2.size(); k++) {
					Cell neighborCell2 = neighborList2.get(k);
					if(neighborCell2.getX() > -1 && neighborCell2.getX() < hexBoard.getSize() &&
					   neighborCell2.getY() > -1 && neighborCell2.getY() < hexBoard.getSize() &&
				       hexBoard.isEmpty(neighborCell2)) {
						Location loc = new Location (neighborCell2.getX(), neighborCell2.getY());
						if(!moveList.contains(loc))
							moveList.add(loc);
					}
					
					//level 3					
					ArrayList<Cell> neighborList3 =  (ArrayList<Cell>) hexBoard.getCellNeighbors(neighborCell);
					for(int l = 0; l < neighborList3.size(); l++) {
						Cell neighborCell3 = neighborList3.get(l);
						if(neighborCell3.getX() > -1 && neighborCell3.getX() < hexBoard.getSize() &&
						   neighborCell3.getY() > -1 && neighborCell3.getY() < hexBoard.getSize() &&
					       hexBoard.isEmpty(neighborCell3)) {
							Location loc = new Location (neighborCell3.getX(), neighborCell3.getY());
							if(!moveList.contains(loc)) moveList.add(loc);
						}
						
					} // lev3						
				} // lev2
			} // lev1
		}
		 return moveList.isEmpty() ? getAllPossibleMoves(hexBoard) : moveList;
	}	
	
	//MergeBoard(): Merge 2 boards information into 1
	public static HexBoard mergeBoard(HexBoard board1, HexBoard board2) {
	  HexBoard board = board1.clone();
	  for(int y = 0; y < board.getSize(); y++) {
	    for(int x = 0; x < board.getSize(); x++) {
	      //if board2.cell has content, and board cell is empty, then add
	      if(board.isEmpty(board.getCell(x, y))) {
	        board.setMove(board2.getCell(x, y).clone());
	      }
	    }
	  }
	  return board;
	}	
	
	
}